package games.tanchishe;

import java.awt.*;

public class Snake {

    private Color color;
    private int length;
    private int width;
    private int weight;
    private int[] bodyX = new int[1000];
    private int[] bodyY = new int[1000];

    private Color[] colors = new Color[1000];


    public Snake(Color color) {
        init();
    }

    public Color getColor(int index){
        return this.colors[index];
    }
    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int[] getBodyX() {
        return bodyX;
    }

    public void setBodyX(int[] bodyX) {
        this.bodyX = bodyX;
    }

    public int[] getBodyY() {
        return bodyY;
    }

    public void setBodyY(int[] bodyY) {
        this.bodyY = bodyY;
    }

    public boolean eat(Food food) {
        int diffX = Math.abs(bodyX[0] - food.getX());
        int diffY = Math.abs(bodyY[0] - food.getY());
        System.out.println("sanke：" +bodyX[0]+","+bodyY[0]+"eat food："+food.getX()+","+food.getY()+" diff0:"+diffX+","+diffY);
        if (diffX <= 5 && diffY <= 5) {
            System.out.println("吃到实物");
            this.length++;
            // 设置吃到的食物颜色
            this.colors[this.length-1] = food.getColor();
            return true;
        }
        return false;
    }

    public boolean isDead() {
        if (length == 3) {
            return false;
        }
        for (int i = 1; i < length ; i++) {
            if (Math.abs(bodyX[0]-bodyX[i]) <= 2 && Math.abs(bodyY[0]-bodyY[i]) <= 2 && i>4){
                return true;
            }
        }
        return false;
    }

    public void init() {
        this.length = 3;
        this.weight = 3;
        bodyX[0] = 50;
        bodyY[0] = 300;
        bodyX[1] = 40;
        bodyY[1] = 300;
        bodyX[2] = 30;
        bodyY[2] = 300;
        colors[0] = Color.RED;
        colors[1] = Color.RED;
        colors[2] = Color.RED;
    }
}
